// pages/exp/psi/precognition.js
var utils = require('../../../utils/util.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        chosen: false,
        imgUrl: '../../../images/amitabha.jpeg',
        btnType: 'default',
        btnText: '下一组',
        btnDisabled: 'true',
        block: {
            up: {
                mask: false,
                imgUrl: ''
            },
            down: {
                mask: false,
                imgUrl: ''
            }
        },
    },
    attendId: -1,
    submitted: false,
    finish: false,

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: ' 超心理学测试'
        })
        var that = this
        utils.httpRequest({
            url: '/PsiPrecognition/nextImg?r=' + Math.random(),
            success: function (res) {
                that.attendId = res.data.attendId
                that.setData({ imgUrl: res.data.image })

                if (parseInt(res.data.step) >= res.data.totalStep) {
                    that.setData({
                        btnText: '完成测试'
                    })
                    that.finish = true;
                }

                wx.setNavigationBarTitle({
                    title: res.data.step + '/' + res.data.totalStep + ' 超心理学测试'
                })

            },
            fail: function () {
                console.log('get data failed')
                wx.showModal({
                    title: '网络错误',
                    content: '无法连接到服务器，请检查您的网络连接或稍后重试',
                    showCancel: false,
                    complete: function () {
                        wx.navigateBack({
                            delta: 1
                        })
                    }
                })
            }
        })
    },

    tapImage: function (res) {
        if(this.submitted) return
        var tapId = res.target.id
        this.selectedImgId = tapId
        var b = this.data.block
        b.up.mask = true
        b.down.mask = true
        b[tapId].mask = false
        this.setData({ chosen: true, block: b })
        var that = this
        this.submit2server(function(hit){
            that.submitted = true
            var id = hit ? tapId : (tapId == 'up' ? 'down' : 'up')
            var oid = hit ? (tapId == 'up' ? 'down' : 'up') : tapId
            b[id].imgUrl = that.data.imgUrl
            b[oid].imgUrl = '../../../images/white.jpg'
            b[id].cls = 'correct'
            // b[id].cls = 'correct'
            if (hit) {
                that.setData({ block: b, btnDisabled: false, btnText: '正确，' + that.data.btnText, btnType: 'primary' })
            }else{
                that.setData({ block: b, btnDisabled: false, btnText: '错误，' + that.data.btnText, btnType: 'warn'})
            }
        })
    },

    commitAndNext: function () {
        if (this.submitted) {
            if (this.finish) {
                var that = this
                wx.redirectTo({
                    url: 'precognition-result?attendID=' + that.attendId,
                })
            } else {
                wx.redirectTo({
                    url: 'precognition',
                })
            }
        } else {
            wx.showModal({
                title: '请选择方块！',
                content: '点击方块后才能提交。必须提交选择后才能查看结果',
                showCancel: false
            })
        }
    },

    submit2server: function (callback) {
        wx.showLoading({
            title: '接收结果中',
            mask: true
        })
        const that = this;
        utils.httpRequest({
            url: '/PsiPrecognition/commitScore',
            method: 'POST',
            data: {
                'attendID': that.attendId,
                'result': that.selectedImgId
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            complete: function (res) {
                wx.hideLoading()
                // wx.showToast({
                //     title: '完成',
                //     icon: 'success',
                //     duration: 800
                // });
                // console.log(res)
                if (callback) callback(res.data);
            }
        })
    },

    help: function () {
        wx.showModal({
            title: '操作说明',
            content: '您可以随时关闭页面，参与结果将为您保存，可返回继续实验。实验结束后您可以查看实验结果。',
            showCancel: false
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return { title: "超心理学能力测试", path: '/pages/exp/psi/precognition', imageUrl: '../../../images/psychic-powers.jpg' }
    }
})